/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.veterinarySystem.view;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.veterinarySystem.bl.DetalleExamenBlLocal;
import mx.its.dsw2.veterinarySystem.bl.ExamenBlLocal;
import mx.its.dsw2.veterinarySystem.bl.MascotaBlLocal;
import mx.its.dsw2.veterinarySystem.enumeraciones.EnumCategoria;
import mx.its.dsw2.veterinarySystem.enumeraciones.EnumTipoExamenLab;
import mx.its.dsw2.veterinarySystem.enumeraciones.EnumVeterinarios;
import mx.its.dsw2.veterinarySystem.modelo.DetalleExamen;
import mx.its.dsw2.veterinarySystem.modelo.Examen;

/**
 *
 * @author 100101
 */
@ManagedBean
@SessionScoped
public class ExamBean {
    @EJB
    private MascotaBlLocal mascotaBl;
    @EJB
    private DetalleExamenBlLocal detalleExamenBl;
    @EJB
    private ExamenBlLocal examenBl;
    
    
    private List<Examen> listExam;
    private List<DetalleExamen>listDetailsPrincipal;
    private List<DetalleExamen>listDetails;
    
    private List<String> listVeterinary;
    private List<String> listCategory;
    private List<String> listTypeExam;
    
    private Examen exam;
    private String accion="";
    private DetalleExamen detailExam;
    
    private String tipoExamen;
    private String categoriaExamen;
    private String resultados;
    private String receta;
    private String recomendaciones;
    private String medicinaPreventiva;
    private boolean visible=false;
    
    private int idMascota;
    
    String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public ExamBean() {
        exam=new Examen();
        detailExam=new DetalleExamen();
        visible=false;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }
    
    

    public List<String> getListVeterinary() {
        listVeterinary= new ArrayList<String>();
                EnumVeterinarios[] listaV=EnumVeterinarios.values();
                for(int x=0;x<listaV.length;x++){
             listVeterinary.add(listaV[x].name());
         }
        return listVeterinary;
    }

    public List<String> getListCategory() {
        listCategory=new ArrayList<String>();
                EnumCategoria[] listaC=EnumCategoria.values();
                    for(int x=0;x<listaC.length;x++){
             listCategory.add(listaC[x].name());
         }    
        return listCategory;
    }

    public List<String> getListTypeExam() {
//        if(categoriaExamen.equalsIgnoreCase("LABORATORIO")){
            listTypeExam=new ArrayList<String>();
            EnumTipoExamenLab[] listaT=EnumTipoExamenLab.values();
            for(int x=0;x<listaT.length;x++){
             listTypeExam.add(listaT[x].name());
         }    
//        }
        return listTypeExam;
    }

    
    
    public int getIdMascota() {
        return idMascota;
    }

    public void setIdMascota(int idMascota) {
        this.idMascota = idMascota;
    }

    public String getTipoExamen() {
        return tipoExamen;
    }

    public void setTipoExamen(String tipoExamen) {
        this.tipoExamen = tipoExamen;
    }

    public String getCategoriaExamen() {
        return categoriaExamen;
    }

    public void setCategoriaExamen(String categoriaExamen) {
        this.categoriaExamen = categoriaExamen;
    }

    public String getResultados() {
        return resultados;
    }

    public void setResultados(String resultados) {
        this.resultados = resultados;
    }

    public String getReceta() {
        return receta;
    }

    public void setReceta(String receta) {
        this.receta = receta;
    }

    public String getRecomendaciones() {
        return recomendaciones;
    }

    public void setRecomendaciones(String recomendaciones) {
        this.recomendaciones = recomendaciones;
    }

    public String getMedicinaPreventiva() {
        return medicinaPreventiva;
    }

    public void setMedicinaPreventiva(String medicinaPreventiva) {
        this.medicinaPreventiva = medicinaPreventiva;
    }

    public Examen getExam() {
        return exam;
    }
      
    public void setExam(Examen exam) {
        this.exam = exam;
    }

    public DetalleExamen getDetailExam() {
        return detailExam;
    }

    public void setDetailExam(DetalleExamen detailExam) {
        this.detailExam = detailExam;
    }
    
    

    public List<Examen> getListExam() {
        listExam=examenBl.getAll();
        return listExam;
    }

    public List<DetalleExamen> getListDetails() {
//        listDetails=detalleExamenBl.getAll();
        return listDetails;
    }

    public List<DetalleExamen> getListDetailsPrincipal() {
//        listDetailsPrincipal=detalleExamenBl.getAll();
        return listDetailsPrincipal;
    }
    
    
    
    public String procesarAccion(){
//        String link="";
        if(accion.equalsIgnoreCase("Nuevo")&&!listDetails.isEmpty()){
            examenBl.register(exam, listDetails);
        }else{
            if(accion.equalsIgnoreCase("Editar")){
                examenBl.modify(exam, listDetails);
            }else{
                if(accion.equalsIgnoreCase("EditarDetail")){
                    detalleExamenBl.modify(detailExam);
                }
            }
        }
        accion="";
        return "ExamList";
    }
    
    public String procesarAccionParaConfirmar(){
        String page="";
        if(!listDetails.isEmpty()&&accion.equalsIgnoreCase("Nuevo")){
        exam.setIdMascota(mascotaBl.getMascotaById(idMascota));
        page= "ExamConfirm";
        }
        else{
            message="REQUIERE AGREGAR POR LO MENOS UN DETALLE AL EXAMEN";
            
        }
        return page;
    }
    
     public String prepararCancelar(){
        String link="";
        if(accion.equalsIgnoreCase("Nuevo")||accion.equalsIgnoreCase("Editar")){
            link="ExamCreateEdit";
        }else{
            if(accion.equalsIgnoreCase("Eliminar")){
                link="ExamList";
            }
        }
        return link;
    }
    
    public void prepararNuevo(ActionEvent evt){
        accion="Nuevo";
        exam=new Examen();
//        detailExam=new DetalleExamen();
        listDetails=new ArrayList<DetalleExamen>();
        tipoExamen="";
        categoriaExamen="";
        resultados="";
        receta="";
        recomendaciones="";
        medicinaPreventiva="";
        message="";
        visible=false;
    }
    
    public void prepararEditar(ActionEvent evt){
        accion="Editar";
        visible=false;
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        exam=listExam.get(index);
        listDetails=detalleExamenBl.getListByExamen(exam);
//        return "ExamCreateEdit";
    }
    
    public void eliminarDetalle(ActionEvent evt){
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        listDetails.remove(index);
    }
    
    public void agregarDetallesDeExamen(ActionEvent evt){
     listDetails.add(new DetalleExamen(null, tipoExamen, categoriaExamen, resultados, receta,recomendaciones,medicinaPreventiva));
     message="";
     resultados="";
     receta="";
     recomendaciones="";
     medicinaPreventiva="";
        }
    
     public void prepararEditarDetalles(ActionEvent evt){
        accion="EditarDetail";
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        detailExam=listDetailsPrincipal.get(index);
        
    }
    
    public String mostrarContenido(){
        visible=true;
        listDetailsPrincipal=detalleExamenBl.getListByExamen(exam);
        return "ExamList";
    }
    
    public String ocultar(){
        visible=false;
        return  "ExamList";
    }
    
    
}
